package com.jf.identity.util;

import com.jf.identity.exception.ServiceException;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.Optional;
import java.util.function.Supplier;

/**
 * @author FCG
 */
public class Params {
    public static void isAnyBlank(Object... args) {
        if (args == null || args.length == 0) {
            throw new IllegalArgumentException("params can't be blank");
        }
        for (Object arg : args) {
            isBlank(arg);
        }
    }
    public static <T> T isNotEmpty(T arg, String name) {
        //arg = Optional.ofNullable(arg).orElseThrow(() -> new IllegalArgumentException(name + "不能为空"));
        if(StringUtils.isEmpty(arg)){
            throw new IllegalArgumentException(name + "不能为空");
        }
        return arg;
    }
    public static <T> T isBlank(T arg, String name) {
        arg = Optional.ofNullable(arg).orElseThrow(() -> new IllegalArgumentException(name + " can't be null"));
        if (arg instanceof CharSequence) {
            CharSequence chars = (CharSequence) arg;
            for (int i = 0; i < chars.length(); i++) {
                if (!Character.isWhitespace(chars.charAt(i))) {
                    return arg;
                }
            }
            throw new IllegalArgumentException(name + " can't be whitespace");
        }
        return arg;
    }

    public static <T> T isBlank(T arg) {
//        return isBlank(arg, "param");
        arg = Optional.ofNullable(arg).orElseThrow(() -> new IllegalArgumentException("param" + " can't be null"));
        if (arg instanceof CharSequence) {
            CharSequence chars = (CharSequence) arg;
            for (int i = 0; i < chars.length(); i++) {
                if (!Character.isWhitespace(chars.charAt(i))) {
                    return arg;
                }
            }
            throw new IllegalArgumentException("param" + " can't be whitespace");
        }
        return arg;
    }

    public static <X extends Throwable, T> T isBlank(T arg, Supplier<? extends X> exSupplier) throws X {
        arg = Optional.ofNullable(arg).orElseThrow(exSupplier);
        if (arg instanceof CharSequence) {
            CharSequence chars = (CharSequence) arg;
            for (int i = 0; i < chars.length(); i++) {
                if (!Character.isWhitespace(chars.charAt(i))) {
                    return arg;
                }
            }
            throw exSupplier.get();
        }
        return arg;
    }

    public static void isAllBlank(Object... args) {
        if (args == null || args.length == 0) {
            throw new IllegalArgumentException("params can't be all blank");
        }
        for (Object arg : args) {
            if (isNotBlank(arg)) {
                return;
            }
        }
        throw new IllegalArgumentException("params can't be all blank");
    }

    public static void beanValidate(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            FieldError fe = bindingResult.getFieldError();
            throw new IllegalArgumentException(fe.getDefaultMessage());
        }
    }

    private static boolean isNotBlank(Object arg) {
        if (arg == null) {
            return false;
        }
        if (arg instanceof CharSequence) {
            CharSequence chars = (CharSequence) arg;
            for (int i = 0; i < chars.length(); i++) {
                if (!Character.isWhitespace(chars.charAt(i))) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }
}

















